package com.such.kit.validate;

import java.text.SimpleDateFormat;
import java.util.Date;
import com.such.kit.Logger;
import com.such.kit.datacast.CastCommons;
import com.such.kit.string.StringCommons;

/**
 * @author SUCH
 * <pre>
 * 数据类型校验工具类
 * </pre>
 */
public class ValidateDataType {

	/**
	 * <pre>
	 * 判断字符串是否为数字
	 * </pre>
	 * @param str 需验证的字符串
	 * @return boolean true 为是
	 */
	public static boolean validateNumber(String str) {
		Logger.trace(ValidateDataType.class, StringCommons.merger("验证字符串 [", str, "] 是否为数字"));
		boolean boole = false;
		if ("NaN".equals(str)) {
			return boole;
		}
		try {
			Double.parseDouble(str);
			boole = true;
		} catch (Exception e) {
			boole = false;
			Logger.trace(ValidateDataType.class, "字符串不是数字", e);
		}
		return boole;
	}

	/**
	 * <pre>
	 * 判断字符串是否为短整型
	 * </pre>
	 * @param str 需验证的字符串
	 * @return boolean true 为是
	 */
	public static boolean validateShort(String str) {
		Logger.trace(ValidateDataType.class, StringCommons.merger("验证字符串 [", str, "] 是否为短整型"));
		boolean boole = false;
		if ("NaN".equals(str)) {
			return boole;
		}
		try {
			Short.parseShort(str);
			boole = true;
		} catch (Exception e) {
			boole = false;
			Logger.trace(ValidateDataType.class, "字符串不是短整型", e);
		}
		return boole;
	}

	/**
	 * <pre>
	 * 判断字符串是否为整型
	 * </pre>
	 * @param str 需验证的字符串
	 * @return boolean true 为是
	 */
	public static boolean validateInteger(String str) {
		Logger.trace(ValidateDataType.class, StringCommons.merger("验证字符串 [", str, "] 是否为整型"));
		boolean boole = false;
		if ("NaN".equals(str)) {
			return boole;
		}
		try {
			Integer.parseInt(str);
			boole = true;
		} catch (Exception e) {
			boole = false;
			Logger.trace(ValidateDataType.class, "字符串不是整型", e);
		}
		return boole;
	}

	/**
	 * <pre>
	 * 判断字符串是否为长整型
	 * </pre>
	 * @param str 需验证的字符串
	 * @return boolean true 为是
	 */
	public static boolean validateLong(String str) {
		Logger.trace(ValidateDataType.class, StringCommons.merger("验证字符串 [", str, "] 是否为长整型"));
		boolean boole = false;
		if ("NaN".equals(str)) {
			return boole;
		}
		try {
			Long.parseLong(str);
			boole = true;
		} catch (Exception e) {
			boole = false;
			Logger.trace(ValidateDataType.class, "字符串不是长整型", e);
		}
		return boole;
	}

	/**
	 * <pre>
	 * 判断字符串是否为单精度浮点型
	 * </pre>
	 * @param str 需验证的字符串
	 * @return boolean true 为是
	 */
	public static boolean validateFloat(String str) {
		Logger.trace(ValidateDataType.class, StringCommons.merger("验证字符串 [", str, "] 是否为单精度浮点型"));
		boolean boole = false;
		if ("NaN".equals(str)) {
			return boole;
		}
		try {
			Float.parseFloat(str);
			boole = true;
		} catch (Exception e) {
			boole = false;
			Logger.trace(ValidateDataType.class, "字符串不是单精度浮点型", e);
		}
		return boole;
	}

	/**
	 * <pre>
	 * 判断字符串是否为双精度浮点型
	 * </pre>
	 * @param str 需验证的字符串
	 * @return boolean true 为是
	 */
	public static boolean validateDouble(String str) {
		Logger.trace(ValidateDataType.class, StringCommons.merger("验证字符串 [", str, "] 是否为双精度浮点型"));
		boolean boole = false;
		if ("NaN".equals(str)) {
			return boole;
		}
		try {
			Double.parseDouble(str);
			boole = true;
		} catch (Exception e) {
			boole = false;
			Logger.trace(ValidateDataType.class, "字符串不是双精度浮点型", e);
		}
		return boole;
	}

	/**
	 * <pre>
	 * 判断字符串是否为时间类型
	 * </pre>
	 * @param str 需验证的字符串
     * @param format 时间格式
	 * @return boolean true 为是
	 */
	public static boolean validataDate(String str, String format) {
		Logger.trace(ValidateDataType.class, StringCommons.merger("验证字符串 [", str, "] 是否符合时间格式 [", format, "]"));
		boolean boole = false;
		try {
			SimpleDateFormat sdf = CastCommons.getSimpleDateFormat(format);
			Date date = sdf.parse(str);
			String checker = sdf.format(date);
			if (str.equals(checker)) {
				boole = true;
			} else {
				boole = false;
				Logger.trace(ValidateDataType.class, StringCommons.merger("字符串 [", str, "] 的时间格式 [", format, "] 验证字符串为 [", checker, "]，二者不匹配"));
			}
		} catch (Exception e) {
			boole = false;
			Logger.trace(ValidateDataType.class, StringCommons.merger("字符串 [", str, "] 不是时间类型"), e);
		}
		return boole;
	}

}
